package com.iot.product.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.iot.commonutils.controller.BaseController;
import com.iot.commonutils.utils.Result;
import com.iot.product.entity.Product;
import com.iot.product.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 产品表(包括网桥产品) 前端控制器
 * </p>
 *
 * @author 孟繁森
 * @since 2021-03-17
 */
@RestController
@RequestMapping("/api/iot-product")
@Api(value = "测试基本增删改查", tags = "测试基本增删改查，之后可删除")
public class ProductController extends BaseController {
    @Autowired
    private ProductService productService;

    @ApiOperation(value = "增加产品")
    @PostMapping("/addProduct")
    public Result addProduct(Product product) {
        boolean b = productService.save(product);
        return b ? Result.success("添加成功") : Result.error("添加失败");
    }

    @ApiOperation(value = "删除产品")
    @DeleteMapping("/deleteProduct/{productId}")
    public Result deleteProduct(@PathVariable String productId) {
        return productService.removeById(productId) ? Result.success("删除成功") : Result.error("删除失败");
    }

    @ApiOperation(value = "修改产品")
    @PostMapping("/updateProduct")
    public Result updateProduct(Product product) {
        boolean b = productService.updateById(product);
        return b ? Result.success("修改成功") : Result.error("修改失败");
    }

    @ApiOperation(value = "查询单个产品")
    @GetMapping("/getProduct/{productId}")
    public Result getProduct(@PathVariable String productId) {
        Map<String, Object> map = new HashMap<>();
        //1.先使用mybatisplus自带的查询一下
        Product product = productService.getById(productId);
//        System.out.println("product: " + product.toString());
        //2.自己定义sql查询
        Product product1 = productService.getById(productId);
        QueryWrapper<Product> wrapper = new QueryWrapper<>();
        wrapper.eq("id", productId);
        IPage<Product> page = productService.page(getPage(Product.class), wrapper);
        map.put("product", product);
        map.put("product1", product1);
        map.put("page", page);
        return Result.success("查询成功", map);
    }


    @ApiOperation(value = "获取产品列表")
    @PostMapping("/getProductList")
    public Result getProductList(@RequestBody Product params) {

        System.out.println("params product is: "+ params.toString());

        IPage<Product> productList = productService.getProductList(getPage(Product.class), params);

        return Result.success("ok", productList);
    }
}

